/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package controlador.facturas;

import controlador.ServletHelper;
import controlador.ventas.AgregarProductoVenta;
import controlador.ventas.MostrarDetalleBoleta;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Collection;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.annotation.Resource;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.sql.DataSource;
import modelo.DetalleBoleta;
import modelo.DetalleFactura;
import modelo.Factura;
import persistencia.DetalleFacturaBean;
import persistencia.FacturaBean;

/**
 *
 * @author Felipe
 */
@WebServlet(name = "AnularFactura", urlPatterns = {"/AnularFactura"})
public class AnularFactura extends HttpServlet {

    @Resource(name = "jdbc/HOLAJDBC")
    private DataSource ds;

    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        Connection conexion = null;
        boolean condi = ServletHelper.validarUsu(request, response);
        if (condi) {
            try {
                HttpSession session = request.getSession();
                session.removeAttribute("Mensaje");
                conexion = ds.getConnection();
                conexion.setAutoCommit(false);
                int corr = 0;
                String strCodigo = request.getParameter("CORR_FACTURA");
                if (strCodigo != null) {
                    corr = Integer.parseInt(strCodigo);
                }
                FacturaBean bean = new FacturaBean(conexion);
                bean.AnularFactura(corr);
                conexion.commit();
                session.setAttribute("Mensaje", "Factura Anulada");
                response.sendRedirect("/TT2/index.jsp");
            } catch (SQLException ex) {
                try {
                    conexion.rollback();
                } catch (SQLException ex1) {
                    Logger.getLogger(AnularFactura.class.getName()).log(Level.SEVERE, null, ex1);
                }
                Logger.getLogger(AgregarProductoVenta.class.getName()).log(Level.SEVERE,
                        "No se pudo obtener la conexión a la base de datos", ex);

            } finally {
                out.close();
                try {
                    conexion.close();

                } catch (SQLException ex) {

                }
            }
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        Connection conexion = null;
        PrintWriter out = response.getWriter();
        boolean condi = ServletHelper.validarUsu(request, response);
        if (condi) {
            try {
                Factura fac;
                conexion = ds.getConnection();
                DetalleFacturaBean bean = new DetalleFacturaBean(conexion);
                FacturaBean facbean = new FacturaBean(conexion);
                int corr = 0;
                String strCod = request.getParameter("CORR_FACTURA");
                if (strCod != null) {
                    request.removeAttribute("Mensaje");
                    corr = Integer.parseInt(strCod);
                }
                fac = facbean.BuscarPorCorr(corr);
                if (fac == null) {
                    request.setAttribute("Mensaje", "Factura no existe");
                    request.getRequestDispatcher("/Facturas/DetalleFacturas.jsp").forward(request, response);
                } else {
                    Collection<DetalleFactura> detalle = bean.obtenerDetalleFactura(fac.getCorrFactura());
                    request.setAttribute("factura", fac);
                    request.setAttribute("detFac", detalle);
                    request.getRequestDispatcher("/Admin/Facturas/AnularFactura.jsp").forward(request, response);
                }

            } catch (SQLException ex) {
                Logger.getLogger(MostrarDetalleBoleta.class.getName()).log(Level.SEVERE,
                        "No se pudo obtener la conexión a la base de datos", ex);
                request.setAttribute("Mensaje", "No se pudo obtener la conexión a la base de datos");
            } finally {
                try {
                    conexion.close();
                } catch (Exception noGestionar) {
                }
            }
        }
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

}
